package com.inkyi.assistant.app.todolist.left;

import com.inkyi.assistant.app.todolist.TodoContext;
import com.inkyi.assistant.app.todolist.TodoServiceApi;
import com.inkyi.assistant.app.todolist.event.TodoEvent;
import com.inkyi.assistant.app.utils.MigLayoutHelper;
import com.inkyi.framework.common.event.InkActionEvent;
import com.inkyi.framework.daqian.share.component.SearchField;
import com.inkyi.framework.daqian.share.jlist.ArrayListCellRenderer;
import com.inkyi.framework.daqian.share.jlist.PkArrayListModel;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

public class TodoLeftPanel extends JPanel {

    private SearchField searchField;

    private JList<GroupItem> fixedList = new JList<>();

    private PkArrayListModel<GroupItem> fixedListModel = new PkArrayListModel<>();

    private JList<GroupItem> groupList = new JList<>();

    private PkArrayListModel<GroupItem> groupListModel = new PkArrayListModel<>();

    /**
     * 添加分组按钮
     */
    private JButton addGroupButton;
    /**
     * 分组弹出菜单
     */
    private JPopupMenu groupPopupMenu;
    /**
     * 删除分组
     */
    private JMenuItem groupDelGroupJmi;
    /**
     * 更新分组
     */
    private JMenuItem groupUpdateGroupJmi;

    public TodoLeftPanel() {
        initComponents();
    }


    private void initComponents() {
        // 设置边框
        this.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.GRAY));
        this.setLayout(MigLayoutHelper.create());
        TodoContext.setTodoLeftPanel(this);
        // 搜索框
        searchField = new SearchField();
        add(searchField, "span, pushx, growx, wrap");
        add(new JSeparator(), "span, pushx, growx, wrap");

        // 固定菜单
        fixedList.setSelectionBackground(new Color(212, 212, 212));
        fixedList.setCellRenderer(new ArrayListCellRenderer<>());
        fixedList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        fixedList.setModel(fixedListModel);

        fixedListModel.addElement(new GroupItem(TodoContext.GROUP_ID_JINTIAN, "今天", TodoContext.GROUP_ICOM_JINTIN));
        fixedListModel.addElement(new GroupItem(TodoContext.GROUP_ID_ZHONGYAO, "重要", TodoContext.GROUP_ICOM_ZHONGYAO));
        fixedListModel.addElement(new GroupItem(TodoContext.GROUP_ID_QUANBU, "全部", TodoContext.GROUP_ICOM_QUANBU));
        fixedListModel.addElement(new GroupItem(TodoContext.GROUP_ID_YIWANCHENG, "已完成", TodoContext.GROUP_ICOM_YIWANCHENG));

        add(fixedList, "span, pushx, growx, wrap");
        add(new JSeparator(), "span, pushx, growx, wrap");

        // 分组菜单
        groupList.setSelectionBackground(new Color(212, 212, 212));
        groupList.setCellRenderer(new ArrayListCellRenderer<>());
        groupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        groupList.setModel(groupListModel);
        // 分组弹出菜单
        groupPopupMenu = new JPopupMenu();
        groupDelGroupJmi = new JMenuItem("删除分组");
        groupUpdateGroupJmi = new JMenuItem("修改分组");
        groupPopupMenu.add(groupDelGroupJmi);
        groupPopupMenu.add(groupUpdateGroupJmi);
        groupList.setComponentPopupMenu(groupPopupMenu);
        add(new JScrollPane(groupList), "span, pushx, pushy, growx, growy, wrap");
        // 新增按钮
        addGroupButton = new JButton("新增分组");
        this.add(addGroupButton, "span, pushx, growx");
        // 初始化事件
        initEventListener();
    }


    public void initEventListener() {
        // 新增分组
        addGroupButton.addActionListener(e -> {
            TodoContext.getEventWrap().fireActionEvent(TodoEvent.of(this, TodoEvent.TYPE_GROUP_NEW));
        });
        //
        groupDelGroupJmi.addActionListener(e -> {
            GroupItem groupItem = TodoContext.getTodoLeftPanel().getGroupList().getSelectedValue();
            Long defaultGroupId = TodoServiceApi.DEFAULT_GROUP.getId();
            if (Objects.equals(groupItem.getId(), defaultGroupId)) {
                JOptionPane.showMessageDialog(TodoContext.getTodoListMain(), "默认分组不能修改");
                return;
            }
            TodoContext.getEventWrap().fireActionEvent(TodoEvent.of(this, TodoEvent.TYPE_GROUP_DEL));
        });
        groupUpdateGroupJmi.addActionListener(e -> {
            GroupItem groupItem = TodoContext.getTodoLeftPanel().getGroupList().getSelectedValue();
            if (groupItem == null) {
                JOptionPane.showMessageDialog(TodoContext.getTodoLeftPanel(), "请选择分组");
                return;
            }
            Long defaultGroupId = TodoServiceApi.DEFAULT_GROUP.getId();
            if (Objects.equals(groupItem.getId(), defaultGroupId)) {
                JOptionPane.showMessageDialog(TodoContext.getTodoListMain(), "默认分组不能修改");
                return;
            }
            String newName = JOptionPane.showInputDialog("请输入新名称", groupItem.getTitle());
            if (newName == null) {
                return;
            }
            if (newName.isEmpty()) {
                JOptionPane.showMessageDialog(TodoContext.getTodoLeftPanel(), "新名称不能为空");
                return;
            }
            groupItem.setTitle(newName);
            TodoContext.getEventWrap().fireActionEvent(TodoEvent.of(this, TodoEvent.TYPE_GROUP_UPDATE, groupItem));
        });

        // 选择监听
        fixedList.addListSelectionListener(e -> {
            if (e.getValueIsAdjusting()) {
                return;
            }
            if (e.getSource() instanceof JList) {
                JList<GroupItem> jList = (JList<GroupItem>) e.getSource();
                GroupItem item = jList.getSelectedValue();
                if (item == null) {
                    return;
                }
                InkActionEvent actionEvent = TodoEvent.of(e.getSource(), TodoEvent.TYPE_RECORD_LIST);
                actionEvent.setParam("groupId", item.getId());
                actionEvent.setParam("sourceGroup", "fixed");
                TodoContext.getEventWrap().fireActionEvent(actionEvent);
            }
        });
        groupList.addListSelectionListener(e -> {
            if (e.getValueIsAdjusting()) {
                return;
            }
            if (e.getSource() instanceof JList) {
                JList<GroupItem> jList = (JList<GroupItem>) e.getSource();
                GroupItem item = jList.getSelectedValue();
                if (item == null) {
                    return;
                }
                InkActionEvent actionEvent = TodoEvent.of(e.getSource(), TodoEvent.TYPE_RECORD_LIST);
                actionEvent.setParam("groupId", item.getId());
                actionEvent.setParam("sourceGroup", "group");
                TodoContext.getEventWrap().fireActionEvent(actionEvent);
            }
        });
    }

    public SearchField getSearchField() {
        return searchField;
    }

    public JList<GroupItem> getGroupList() {
        return groupList;
    }

    public PkArrayListModel<GroupItem> getGroupListModel() {
        return groupListModel;
    }

    public JList<GroupItem> getFixedList() {
        return fixedList;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TodoLeftPanel panel = new TodoLeftPanel();
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);

    }
}
